package tt.lab.pmbok.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import tt.lab.pmbok.dto.Tree;
import tt.lab.pmbok.model.Area;
import tt.lab.pmbok.model.Inout;
import tt.lab.pmbok.model.PmbokProcess;
import tt.lab.pmbok.model.ProcessInout;
import tt.lab.pmbok.model.ProcessTool;
import tt.lab.pmbok.model.Tool;
import tt.lab.pmbok.service.AreaService;

@Controller
// 类似Struts的Action
public class AreaController {
	/**/
	@Resource(name = "areaService")
	// 获取applicationContext.xml中bean的id为loginService的，并注入
	private AreaService areaService; // 等价于spring传统注入方式写get和set方法，这样的好处是简洁工整，省去了不必要得代码

	@RequestMapping("/areas")
	public void retrieveAll(Model model) {
		List<Area> areas = areaService.loadAll();
		
		Tree rootNode = new Tree(0, "ROOT", "R", false, false);
		for (Area area : areas) {
			Tree areaNode = new Tree(100000+area.getId(), area.getId()+" "+area.getName(), "A", false, false);
			
			for (PmbokProcess process : area.getSortedProcesses()) {
				Tree processNode = new Tree(10000+process.getId(), process.getDisplayName(), "P", false, false);
				
				//inputs
				Tree inputs = new Tree(1000 + process.getId(), "输入", "IS", false, false);
				for (ProcessInout processInout : process.getIns()) {
					Inout inout = processInout.getInout();
					Tree inputNode = new Tree(100+inout.getId(),inout.getName(),"I", false, true);
					inputs.addChild(inputNode);
				}
				processNode.addChild(inputs);
				
				//tools
				Tree tools = new Tree(2000 + process.getId(), "工具和技术", "TS", false, false);
				for (ProcessTool processTool : process.getTools()) {
					Tool tool = processTool.getTool();
					Tree toolNode = new Tree(200+tool.getId(),tool.getName(),"T", false, true);
					tools.addChild(toolNode);
				}
				processNode.addChild(tools);
				
				//outputs
				Tree outputs = new Tree(3000 + process.getId(), "输出", "OS", false, false);
				for (ProcessInout processInout : process.getOuts()) {
					Inout inout = processInout.getInout();
					Tree outputNode = new Tree(100+inout.getId(),inout.getName(),"O", false, true);
					outputs.addChild(outputNode);
				}
				processNode.addChild(outputs);
				
				areaNode.addChild(processNode);
			}
			rootNode.addChild(areaNode);
		}
		model.addAttribute("children",rootNode.getChildren());
	}

}